package com.sansecy.monitor.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sansecy.monitor.R;
import com.sansecy.monitor.see.ActivityManger;
import com.sansecy.monitor.utils.Utils;

import java.util.List;

/**
 * @Description TODO
 * @Date 2019-08-04 20:56
 * @Created by caoyu
 */
public class SettingDialog extends Dialog implements View.OnClickListener {

    public SettingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_setting);
        TextView tvActivity = findViewById(R.id.tv_activity);
        tvActivity.setOnClickListener(this);
        TextView tvFragment = findViewById(R.id.tv_fragments);
        tvFragment.setOnClickListener(this);
        TextView tvDevicesInfo = findViewById(R.id.tv_devices_info);
        tvDevicesInfo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_activity) {
            ListDialog dialog = new ListDialog(ActivityManger.getTopActivity());
            TextView textView = (TextView) View.inflate(getContext(), android.R.layout.simple_list_item_1, null);
            textView.setText("Activity");
            textView.setTextColor(Color.BLACK);
            dialog.addHeaderView(textView);

            List<Activity> activityList = ActivityManger.getActivitys();
            for (int i = 0; i < activityList.size(); i++) {
                ComponentName componentName = activityList.get(i).getComponentName();
                dialog.addData(componentName.getClassName());
            }

            dialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            });
            dialog.show();
        }
        if (id == R.id.tv_fragments) {
            int dp15 = Utils.dpToPx(getContext(), 15);
            int dp8 = Utils.dpToPx(getContext(), 8);
            final ListDialog dialog = new ListDialog(ActivityManger.getTopActivity());
            LinearLayout linearLayout = new LinearLayout(getContext());
//            ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            params.bottomMargin= dp15;
//            linearLayout.setLayoutParams(params);
            linearLayout.setOrientation(LinearLayout.VERTICAL);

            TextView textView = (TextView) View.inflate(getContext(), android.R.layout.simple_list_item_1, null);
            textView.setText("Fragment");
            textView.setTextColor(Color.BLACK);
            linearLayout.addView(textView);

            HorizontalScrollView scrollView = new HorizontalScrollView(getContext());
            scrollView.setHorizontalScrollBarEnabled(false);
            RadioGroup radioGroup = new RadioGroup(getContext());
            scrollView.addView(radioGroup);
//            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            params.leftMargin = dp8;
//            params.rightMargin = dp8;
//            radioGroup.setLayoutParams(params);
            radioGroup.setPadding(dp8, 0, dp8, 0);

            radioGroup.setOrientation(LinearLayout.HORIZONTAL);
            final RadioButton buttonAll = new RadioButton(getContext());
            buttonAll.setText("全部");
            radioGroup.addView(buttonAll);
            final RadioButton buttonHide = new RadioButton(getContext());
            buttonHide.setText("已隐藏");
            radioGroup.addView(buttonHide);
            final RadioButton buttonShow = new RadioButton(getContext());
            buttonShow.setText("已显示");
            radioGroup.addView(buttonShow);
            final RadioButton buttonVisible = new RadioButton(getContext());
            buttonVisible.setText("已可见");
            radioGroup.addView(buttonVisible);
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    List<Fragment> fragmentList = ActivityManger.getFragments();
                    dialog.clear();
                    Log.d(this.getClass().getName(), "onCheckedChanged:checkedId " + checkedId);
                    if (checkedId == buttonAll.getId()) {
                        for (int i = 0; i < fragmentList.size(); i++) {
                            Class<? extends Fragment> aClass = fragmentList.get(i).getClass();
                            dialog.addData(aClass.getName());
                        }
                    }
                    if (checkedId == buttonHide.getId()) {
                        for (int i = 0; i < fragmentList.size(); i++) {
                            Fragment fragment = fragmentList.get(i);
                            if (fragment.isHidden()) {
                                Class<? extends Fragment> aClass = fragment.getClass();
                                dialog.addData(aClass.getName());
                            }
                        }
                    }
                    if (checkedId == buttonShow.getId()) {
                        for (int i = 0; i < fragmentList.size(); i++) {
                            Fragment fragment = fragmentList.get(i);
                            if (!fragment.isHidden()) {
                                Class<? extends Fragment> aClass = fragment.getClass();
                                dialog.addData(aClass.getName());
                            }
                        }
                    }
                    if (checkedId == buttonVisible.getId()) {
                        for (int i = 0; i < fragmentList.size(); i++) {
                            Fragment fragment = fragmentList.get(i);
                            if (fragment.isVisible()) {
                                Class<? extends Fragment> aClass = fragment.getClass();
                                dialog.addData(aClass.getName());
                            }
                        }
                    }
                }
            });
            linearLayout.addView(scrollView);
            radioGroup.check(buttonAll.getId());
            dialog.addHeaderView(linearLayout);

            dialog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            });
            dialog.show();
        }
        if (id == R.id.tv_devices_info) {
            DeviceInfoDialog dialog = new DeviceInfoDialog(getContext());
            dialog.show();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window win = getWindow();
        DisplayMetrics dm = new DisplayMetrics();
        // 一定要设置Background，如果不设置，window属性设置无效
        assert win != null;
//        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        win.getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams params = win.getAttributes();
        //设置dialog高度
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //设置dialog宽度
        params.width = (int) (Utils.getScreenWidth(getContext()) * 0.85);
        win.setAttributes(params);
    }
}
